package com.example.anan.AAChartCore.ChartsDemo.AdditionalContent.slice;

import com.example.anan.AAChartCore.AAChartCoreLib.AAChartCreator.AAChartModel;
import com.example.anan.AAChartCore.AAChartCoreLib.AAChartCreator.AAChartView;
import com.example.anan.AAChartCore.AAChartCoreLib.AAChartCreator.AAMoveOverEventMessageModel;
import com.example.anan.AAChartCore.AAChartCoreLib.AAChartCreator.AASeriesElement;
import com.example.anan.AAChartCore.AAChartCoreLib.AAChartEnum.AAChartFontWeightType;
import com.example.anan.AAChartCore.AAChartCoreLib.AAChartEnum.AAChartStackingType;
import com.example.anan.AAChartCore.AAChartCoreLib.AAChartEnum.AAChartType;
import com.example.anan.AAChartCore.AAChartCoreLib.AAOptionsModel.AADataLabels;
import com.example.anan.AAChartCore.AAChartCoreLib.AAOptionsModel.AAStyle;
import com.example.anan.AAChartCore.AAChartCoreLib.AATools.AAColor;
import com.example.anan.AAChartCore.AAChartCoreLib.AATools.AAGradientColor;
import com.example.anan.AAChartCore.ResourceTable;
import com.example.anan.AAChartCore.utils.Log;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

public class EvaluateJSStringFunctionAbilitySlice extends AbilitySlice implements AAChartView.AAChartViewCallBack {

    private AAChartView aaChartView;
    private String chartType;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_evaluate_jsstring_function);
        try {
            int color = getResourceManager().getElement(ResourceTable.Color_status_bar_color).getColor();
            getWindow().setStatusBarColor(color);
        } catch (Exception e) {
            e.printStackTrace();
        }
        chartType = intent.getStringParam("chartType");

        aaChartView = (AAChartView) findComponentById(ResourceTable.Id_AAChartView);
        AAChartModel aaChartModel = configureChartModel();
        aaChartView.aa_drawChartWithChartModel(aaChartModel);
        aaChartView.callBack = this;
    }

    AAChartModel configureChartModel() {
        if (chartType.equals("evalJSFunction3")) {
            return configureStackingColumnMixedLineChart();
        }

        return new AAChartModel()
                .chartType(AAChartType.Line)
                .title("")
                .yAxisTitle("")
                .dataLabelsEnabled(true)
                .tooltipEnabled(true)
                .backgroundColor(AAGradientColor.OceanBlue)
                .series(new AASeriesElement[]{
                        new AASeriesElement()
                                .name("Tokyo")
                                .data(new Object[]{29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4})
                                .color(AAGradientColor.DeepSea)

                });
    }

    AAChartModel configureStackingColumnMixedLineChart() {
        AASeriesElement[] aaSeriesArr = {
                new AASeriesElement()
                        .name("新用户")
                        .data(new Object[]{82.89, 67.54, 62.07, 59.43, 67.02, 67.09, 35.66, 71.78, 81.61, 78.85, 79.12, 72.30})
                        .dataLabels(
                        new AADataLabels()
                                .enabled(true)
                                .style(new AAStyle()
                                        .color(AAColor.Red)
                                        .fontSize(11f)
                                )
                )
                ,
                new AASeriesElement()
                        .name("老用户")
                        .data(new Object[]{198.66, 330.81, 151.95, 160.12, 222.56, 229.05, 128.53, 250.91, 224.47, 473.99, 126.85, 260.50})
                        .dataLabels(
                        new AADataLabels()
                                .enabled(true)
                                .style(new AAStyle()
                                        .color("#000000")
                                        .fontSize(11f)
                                )
                ),
                new AASeriesElement()
                        .name("总量")
                        .type(AAChartType.Line)
                        .data(new Object[]{281.55, 398.35, 214.02, 219.55, 289.57, 296.14, 164.18, 322.69, 306.08, 552.84, 205.97, 332.79})
                        .dataLabels(
                        new AADataLabels()
                                .enabled(true)
                                .style(new AAStyle()
                                        .color("#000000")
                                        .fontSize(15f)
                                        .fontWeight(AAChartFontWeightType.Bold)
                                )
                )
                ,
        };
        return new AAChartModel()
                .title("16年1月-16年11月充值客单分析")//图形标题
                .subtitle("BY MICVS")//图形副标题
                .chartType(AAChartType.Column)
                .stacking(AAChartStackingType.Normal)
                .legendEnabled(true)
                .colorsTheme(new Object[]{
                        AAGradientColor.OceanBlue,
                        AAGradientColor.Sanguine,
                        AAGradientColor.LusciousLime,
                })
                .series(aaSeriesArr);
    }

    @Override
    public void chartViewDidFinishLoad(AAChartView aaChartView) {
        Log.i("hh", "🔥🔥🔥🔥🔥 AAChartView content did finish load!!!");

        String jsFunctionStr;

        if (chartType.equals("evalJSFunction1")) {
            jsFunctionStr = configureMaxMiniDataLabelJSFunctionString();
        } else if (chartType.equals("evalJSFunction2")) {
            jsFunctionStr = configureFirstSecondThirdDataLabelJSFunctionString();
        } else {
            jsFunctionStr = configureFirstSecondThirdStackLabelJSFunctionString();
        }

        aaChartView.aa_evaluateTheJavaScriptStringFunction(jsFunctionStr);
    }

    @Override
    public void chartViewMoveOverEventMessage(AAChartView aaChartView, AAMoveOverEventMessageModel messageModel) {

    }

    String configureMaxMiniDataLabelJSFunctionString() {
        String jsStr =
                " function render(aaGlobalChart, point, text) {\n" +
                        "        return aaGlobalChart.renderer.label(text + ': ' + point.y,  point.plotX + aaGlobalChart.plotLeft -20 , point.plotY + aaGlobalChart.plotTop - 45, 'callout', point.plotX + aaGlobalChart.plotLeft, point.plotY + aaGlobalChart.plotTop)\n" +
                        "        .css({\n" +
                        "        color: '#FFFFFF',\n" +
                        "        align: 'center',\n" +
                        "        })\n" +
                        "        .attr({\n" +
                        "        fill: 'rgba(0, 0, 0, 0.75)',\n" +
                        "        padding: 8,\n" +
                        "        r: 5,\n" +
                        "        zIndex: 6\n" +
                        "        })\n" +
                        "        .add();\n" +
                        "        }\n" +
                        "        function renderMinMaxLabel(aaGlobalChart) {\n" +
                        "        var min = 1000,\n" +
                        "        max = 0,\n" +
                        "        pointsToShow = [0, 0],\n" +
                        "        points = aaGlobalChart.series[0].points;\n" +
                        "        Highcharts.each(points, function(p) {\n" +
                        "        if(p.y>max) {\n" +
                        "        pointsToShow[0] = p.index;\n" +
                        "        max = p.y;\n" +
                        "        }\n" +
                        "        if(p.y<min) {\n" +
                        "        pointsToShow[1] = p.index;\n" +
                        "        min = p.y;\n" +
                        "        }\n" +
                        "        });\n" +
                        "        render(aaGlobalChart, points[pointsToShow[0]], 'Max');\n" +
                        "        render(aaGlobalChart, points[pointsToShow[1]], 'Min');\n" +
                        "        }\n" +
                        "        \n" +
                        "        renderMinMaxLabel(aaGlobalChart);";
        return jsStr;
    }

    String configureFirstSecondThirdDataLabelJSFunctionString() {
        Integer firstMaxNumberIndex = 8;
        Integer secondMaxNumberIndex = 9;
        Integer thirdMaxNumberIndex = 5;

        String jsStr =
                "function render(aaGlobalChart, point, text) {\n" +
                        "        return aaGlobalChart.renderer.label(text + ': ' + point.y,  point.plotX + aaGlobalChart.plotLeft -20 , point.plotY + aaGlobalChart.plotTop - 45, 'callout', point.plotX + aaGlobalChart.plotLeft, point.plotY + aaGlobalChart.plotTop)\n" +
                        "        .css({\n" +
                        "        color: '#FFFFFF',\n" +
                        "        align: 'center',\n" +
                        "        })\n" +
                        "        .attr({\n" +
                        "        fill: 'rgba(0, 0, 0, 0.75)',\n" +
                        "        padding: 8,\n" +
                        "        r: 5,\n" +
                        "        zIndex: 6\n" +
                        "        })\n" +
                        "        .add();\n" +
                        "        }\n" +
                        "        function renderFirstSecondThirdLabel(aaGlobalChart) {\n" +
                        "        var points = aaGlobalChart.series[0].points;\n" +
                        "        render(aaGlobalChart, points[firstMaxNumberIndex], '第一名');\n" +
                        "        render(aaGlobalChart, points[secondMaxNumberIndex], '第二名');\n" +
                        "        render(aaGlobalChart, points[thirdMaxNumberIndex], '第三名');\n" +
                        "        }\n" +
                        "        \n" +
                        "        renderFirstSecondThirdLabel(aaGlobalChart);";

        String firstMaxNumberIndexStr = String.valueOf(firstMaxNumberIndex);
        String secondMaxNumberIndexStr = String.valueOf(secondMaxNumberIndex);
        String thirdMaxNumberIndexStr = String.valueOf(thirdMaxNumberIndex);

        jsStr = jsStr.replace("firstMaxNumberIndex", firstMaxNumberIndexStr);
        jsStr = jsStr.replace("secondMaxNumberIndex", secondMaxNumberIndexStr);
        jsStr = jsStr.replace("thirdMaxNumberIndex", thirdMaxNumberIndexStr);

        return jsStr;
    }

    String configureFirstSecondThirdStackLabelJSFunctionString() {
        Integer firstMaxNumberIndex = 9;
        Integer secondMaxNumberIndex = 1;
        Integer thirdMaxNumberIndex = 11;

        String jsStr =
                "function render(aaGlobalChart, point, text,colorIndex) {\n" +
                        "        var colorsArray = [\"#1e90ff\",\"#ef476f\",\"#ffd066\",];\n" +
                        "        var colorHexString = colorsArray[colorIndex];\n" +
                        "        return aaGlobalChart.renderer.label('   '+text,  point.plotX + aaGlobalChart.plotLeft -20 , point.plotY + aaGlobalChart.plotTop - 60, 'callout', point.plotX + aaGlobalChart.plotLeft, point.plotY + aaGlobalChart.plotTop)\n" +
                        "        .css({\n" +
                        "        color: '#FFFFFF',\n" +
                        "        align: 'center',\n" +
                        "        weight: 'bold',\n" +
                        "        })\n" +
                        "        .attr({\n" +
                        "        fill: colorHexString,\n" +
                        "        padding: 8,\n" +
                        "        r: 5,\n" +
                        "        zIndex: 6\n" +
                        "        })\n" +
                        "        .add();\n" +
                        "        }\n" +
                        "        function renderFirstSecondThirdLabel(aaGlobalChart) {\n" +
                        "        var points = aaGlobalChart.series[0].points;\n" +
                        "        render(aaGlobalChart, points[firstMaxNumberIndex], '第一名',0);\n" +
                        "        render(aaGlobalChart, points[secondMaxNumberIndex], '第二名',1);\n" +
                        "        render(aaGlobalChart, points[thirdMaxNumberIndex], '第三名',2);\n" +
                        "        }\n" +
                        "        \n" +
                        "        renderFirstSecondThirdLabel(aaGlobalChart);";

        String firstMaxNumberIndexStr = String.valueOf(firstMaxNumberIndex);
        String secondMaxNumberIndexStr = String.valueOf(secondMaxNumberIndex);
        String thirdMaxNumberIndexStr = String.valueOf(thirdMaxNumberIndex);

        jsStr = jsStr.replace("firstMaxNumberIndex", firstMaxNumberIndexStr);
        jsStr = jsStr.replace("secondMaxNumberIndex", secondMaxNumberIndexStr);
        jsStr = jsStr.replace("thirdMaxNumberIndex", thirdMaxNumberIndexStr);

        return jsStr;
    }


}
